/**
 * @product: PCDC-CMS
 * @copyright: FSAI-南征北战信息科技
 * @filename: com.ruoyi.cmsbiz.util.BeanMapTool.java
 *
 * @link: Http://www.fastsai.com
 * @jdkv: Jdk 1.8
 */
package com.fsai.cms.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.cglib.beans.BeanMap;

/**
 * bean和map互转，性能最佳
 * 
 * @project: pcdc-cms-biz
 *
 * @author: JaffeHe
 * @email: JaffeHe@foxmail.com
 * @date: 2021-03-11 16:00
 *
 * @ChangeLog<br/>
 * 				Developer Date Comment <br/>
 */
public class BeanMapTool {

	@SuppressWarnings("unchecked")
	public static <T> Map<String, ?> beanToMap(T bean) {
		BeanMap beanMap = BeanMap.create(bean);
		Map<String, Object> map = new HashMap<>();

		beanMap.forEach((key, value) -> {
			map.put(String.valueOf(key), value);
		});
		return map;
	}

	public static <T> T mapToBean(Map<String, ?> map, Class<T> clazz)
			throws IllegalAccessException, InstantiationException {
		T bean = clazz.newInstance();
		BeanMap beanMap = BeanMap.create(bean);
		beanMap.putAll(map);
		return bean;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static <T> T sqlMapToBean(Map<String, ?> map, Class<T> clazz)
			throws IllegalAccessException, InstantiationException {
		Map<String, Object> re_map = new HashMap();
		if (re_map != null) {
			Iterator var2 = map.entrySet().iterator();
			while (var2.hasNext()) {
				Entry<String, Object> entry = (Entry) var2.next();
				re_map.put(lineToHump((String) entry.getKey()), map.get(entry.getKey()));
			}
		}

		T bean = clazz.newInstance();
		BeanMap beanMap = BeanMap.create(bean);
		beanMap.putAll(re_map);
		return bean;
	}

	public static <T> List<Map<String, ?>> objectsToMaps(List<T> objList) {
		List<Map<String, ?>> list = new ArrayList<>();
		if (objList != null && objList.size() > 0) {
			Map<String, ?> map = null;
			T bean = null;
			for (int i = 0, size = objList.size(); i < size; i++) {
				bean = objList.get(i);
				map = beanToMap(bean);
				list.add(map);
			}
		}
		return list;
	}

	public static <T> List<T> mapsToObjects(List<Map<String, ?>> maps, Class<T> clazz)
			throws InstantiationException, IllegalAccessException {
		List<T> list = new ArrayList<>();
		if (maps != null && maps.size() > 0) {
			Map<String, ?> map = null;
			for (int i = 0, size = maps.size(); i < size; i++) {
				map = maps.get(i);
				T bean = mapToBean(map, clazz);
				list.add(bean);
			}
		}
		return list;
	}

	private static Pattern linePattern = Pattern.compile("_(\\w)");

	/** 下划线转驼峰 */
	public static String lineToHump(String str) {
		str = str.toLowerCase();
		Matcher matcher = linePattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
}
